package it.webFlora.Dao;
import it.webFlora.bean.db.Utente.*;

import java.io.Serializable;
import java.util.*;
import java.sql.*;


public class UtenteTempDAO {
	
	
	private PreparedStatement pstm = null;
	private Connection conn = null;
	private ResultSet resultSet = null;
	
	

	
	public ResultSet resultSet(){
		String query="SELECT * FROM utente_temp";
		 try {
		 pstm=getPreparedStatement(query);
		resultSet=pstm.executeQuery();
		} catch (SQLException ex) {
		ex.printStackTrace(); }
		return resultSet;
		   }
	
	public String resultSetUtente(String UserName){
		String query="SELECT * FROM utente_temp where nd='N' and user_name=?";
		String userName=null;
		 try {
		 pstm=getPreparedStatement(query);
		 pstm.setString(1,UserName);
		resultSet=pstm.executeQuery();
		if(resultSet!=null){
		   while (resultSet.next())
		    {
			userName=resultSet.getString("user_name");
		    }
		 }
		} catch (SQLException ex) {
		ex.printStackTrace(); }
		return userName;
		   }


		public void getInsertSave(UtenteTemp utenteTemp){
		String query="INSERT INTO utente_temp(id_utente,nome,cognome,user_name,password,ripeti_password," +
				"codice_attivazione,telefono,email,nd)"+
		     " VALUES(?,?,?,?,?,?,?,?,?,?)";

		 try {
		 pstm=getPreparedStatement(query);
		 pstm.setInt(1,utenteTemp.getId_utente());
		 pstm.setString(2,utenteTemp.getNome());
		 pstm.setString(3,utenteTemp.getCognome());
		 pstm.setString(4,utenteTemp.getUser_name());
		 pstm.setString(5,utenteTemp.getPassword());
		 pstm.setString(6,utenteTemp.getRipeti_password());
		 pstm.setInt(7,utenteTemp.getCodice_attivazione());
		 pstm.setInt(8,utenteTemp.getTelefono());
		 pstm.setString(9,utenteTemp.getEmail());
		 pstm.setString(10,utenteTemp.getNd());
		pstm.executeUpdate();

		} catch (SQLException ex) {
		ex.printStackTrace(); }
		finally{
		closeSession();
		}
		   }



		public void saveUpdate(UtenteTemp utenteTemp){
		String query="UPDATE utente_temp SET id_utente=?,nome=?,cognome=?,user_name=?,password=?,ripeti_password=?,codice_attivazione=?,telefono=?,email=?,nd=? WHERE id_utente='"+utenteTemp.getId_utente()+"'";

		 try {
		 pstm=getPreparedStatement(query);

		 pstm.setInt(1,utenteTemp.getId_utente());
		 pstm.setString(2,utenteTemp.getNome());
		 pstm.setString(3,utenteTemp.getCognome());
		 pstm.setString(4,utenteTemp.getUser_name());
		 pstm.setString(5,utenteTemp.getPassword());
		 pstm.setString(6,utenteTemp.getRipeti_password());
		 pstm.setInt(7,utenteTemp.getCodice_attivazione());
		 pstm.setInt(8,utenteTemp.getTelefono());
		 pstm.setString(9,utenteTemp.getEmail());
		 pstm.setString(10,utenteTemp.getNd());
		pstm.executeUpdate();

		} catch (SQLException ex) {
		ex.printStackTrace(); }
		finally{
		closeSession();
		}
		   }




		public void deleteUtenza(String user){
		String query="DELETE FROM utente_temp WHERE user_name='"+user+"'";
		 try {
		 pstm=getPreparedStatement(query);
		pstm.executeUpdate();

		} catch (SQLException ex) {
		ex.printStackTrace(); }
		finally{
		closeSession();
		}

		}
		public PreparedStatement getPreparedStatement(String query){
			try {

			getConnection();
			pstm=conn.prepareStatement(query);
			 }
			
			catch (SQLException ex) {
			ex.printStackTrace(); }
			return pstm;}
			 

		 public Connection getConnection()
           {
	        try {

		Class.forName("com.mysql.jdbc.Driver");
		conn=DriverManager.getConnection("jdbc:mysql://localhost/webflora","root","");
		System.out.println("Connessione avvenuta !!");
		 }	
	    catch (ClassNotFoundException e) {
		e.printStackTrace(); }
		catch (SQLException ex) {
		ex.printStackTrace(); }
		return conn;
		}
  


			public void closeSession(){
			try {
			conn.close();
			pstm.close();
			if(resultSet!=null){
			resultSet.close();
			}
			}catch (SQLException ex) {
			ex.printStackTrace(); }
			}
			
			public String getMaxIdUtente(){
				int idUser=0;
				String stUser=null;
				String query="SELECT max(id_utente) FROM utente_temp";
				 try {
				 pstm=getPreparedStatement(query);
				resultSet=pstm.executeQuery();
				if(resultSet.next())
				{
					stUser=resultSet.getString(1);
				}
				} catch (SQLException ex) {
				ex.printStackTrace(); }
				closeSession();
				return stUser;
				   }
			
			
			
			public int resultIntCodAttivazione(String UserName){
				int codiceAttivazione=0;
				
				String query="SELECT codice_attivazione FROM utente_temp where user_name=?";
				 try {
				 pstm=getPreparedStatement(query);
				 pstm.setString(1,UserName);
				resultSet=pstm.executeQuery();
				 while(resultSet.next())
				 {
					 codiceAttivazione=resultSet.getInt("codice_attivazione");
				 }
				 } catch (SQLException ex) {
				ex.printStackTrace(); }
				return codiceAttivazione;
				   }
			
			
			
			public String controlloUtente(String UserName){
				String query="SELECT * FROM utente_temp where user_name=?";
				String userName=null;
				 try {
				 pstm=getPreparedStatement(query);
				 pstm.setString(1,UserName);
				resultSet=pstm.executeQuery();
				if(resultSet!=null){
				   while (resultSet.next())
				    {
					userName=resultSet.getString("user_name");
				    }
				 }
				} catch (SQLException ex) {
				ex.printStackTrace(); }
				return userName;
				   }
			
			public void UpdateCompletaRegistrazione(String userName){
				String query="UPDATE utente_temp SET nd='S' WHERE user_name='"+userName+"'";

				 try {
				 pstm=getPreparedStatement(query);
				 pstm.executeUpdate();

				} catch (SQLException ex) {
				ex.printStackTrace(); }
				finally{
				closeSession();
				}
				   }
			
			public UtenteTemp findUtenteByUserName(String UserName){
				UtenteTemp utente=new UtenteTemp();
				String query="SELECT * FROM utente_temp where user_name=?";
				String userName=null;
				 try {
				 pstm=getPreparedStatement(query);
				 pstm.setString(1,UserName);
				resultSet=pstm.executeQuery();
				if(resultSet!=null){
				   while (resultSet.next())
				    {
					utente.setUser_name(resultSet.getString("user_name"));
					utente.setNome(resultSet.getString("nome"));
					utente.setCognome(resultSet.getString("cognome"));
					utente.setEmail(resultSet.getString("email"));
					
				    }
				 }
				} catch (SQLException ex) {
				ex.printStackTrace(); }
				return utente;
				   }

			
			public void modifyUpdate(UtenteTemp utenteTemp){
				String query="UPDATE utente_temp SET nome='"+utenteTemp.getNome()+"',cognome='"+utenteTemp.getCognome()+"',telefono=" +
						"?,email='"+utenteTemp.getEmail()+"' WHERE user_name='"+utenteTemp.getUser_name()+"'";

				 try {
				 pstm=getPreparedStatement(query);
				 pstm.setInt(1,utenteTemp.getTelefono());
    		     pstm.executeUpdate();

				} catch (SQLException ex) {
				ex.printStackTrace(); }
				finally{
				closeSession();
				}
				   }

			
}
		
		
